package com.kb.design.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.constant.Constants;
import com.kb.design.dto.BonusConfigDTO;
import com.kb.design.entity.BonusConfig;
import com.kb.design.entity.CustomUser;
import com.kb.design.mapper.postgreSQL.BonusConfigMapper;
import com.kb.design.service.BonusConfigService;
import com.kb.design.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;

@Service
@DS("slave-1")
public class BonusConfigServiceImpl extends ServiceImpl<BonusConfigMapper, BonusConfig> implements BonusConfigService {

    @Resource
    private BonusConfigMapper bonusConfigMapper;

    @Override
    public IPage<BonusConfig> getBonusConfigByCondition(Page<BonusConfig> page, BonusConfigDTO bonusConfig) {
        QueryWrapper<BonusConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", Constants.DEL_FLAG_NO);
        queryWrapper.like(StringUtils.isNotEmpty(bonusConfig.getBonusName()), "bonus_name", bonusConfig.getBonusName());
        queryWrapper.eq(StringUtils.isNotEmpty(bonusConfig.getUserCode()),"user_id", bonusConfig.getUserCode());

        if (bonusConfig.getPinNum() != null) {
            queryWrapper.eq("pin_num", bonusConfig.getPinNum());
        }
        if (bonusConfig.getDesignBonus() != null) {
            queryWrapper.eq("design_bonus", bonusConfig.getDesignBonus());
        }
        if (bonusConfig.getSalesArea() != null) {
            queryWrapper.eq("sales_area", bonusConfig.getSalesArea());
        }
        if (bonusConfig.getSalesCommission() != null) {
            queryWrapper.eq("sales_commission", bonusConfig.getSalesCommission());
        }
        return page(page, queryWrapper);
    }

    @Override
    public boolean saveBonusConfig(BonusConfig bonusConfig) {
        return save(bonusConfig);
    }

    @Override
    public boolean updateBonusConfig(BonusConfig bonusConfig) {
        return updateById(bonusConfig);
    }

    @Override
    public BonusConfig getBonusConfigById(Long id) {
        return getById(id);
    }

    @Override
    public boolean deleteById(Long id) {
        BonusConfig bonusConfig = new BonusConfig();
        bonusConfig.setId(id);
        bonusConfig.setDelFlag(Constants.DEL_FLAG_YES);
        return bonusConfigMapper.updateById(bonusConfig) > 0;
    }
}